package com.asia.algorithmcode.array;

/**
 * @DESCRIPTION: 81 搜索旋转数组2
 * @USER: wanfu
 * @DATE: 2025/4/1 星期二 15:42
 */
public class Search2 {


    public static void main(String[] args) {
        int[] arr = {1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1};
        System.out.println(new Search2().search(arr, 2));
    }


    public boolean search(int[] nums, int target) {
        int index = 1;
        while (index < nums.length && nums[index] >= nums[index - 1]) {
            index++;
        }
        return bSearch(nums, target, 0, index - 1) || bSearch(nums, target, index, nums.length - 1);
    }

    public boolean bSearch(int[] nums, int target, int start, int end) {
        if (start <= end) {
            int mid = start + (end - start) / 2;
            if (nums[mid] == target) {
                return true;
            } else if (nums[mid] > target) {
                end = mid - 1;
                return bSearch(nums, target, start, end);
            } else {
                start = mid + 1;
                return bSearch(nums, target, start, end);
            }
        }
        return false;
    }


}
